package com.app.dao.zl;

import com.app.domain.zl.TensionUploadRecord;
import com.app.domain.zl.ZljlData;
import com.app.domain.zl.ZljlDataUploadStatus;
import com.app.domain.zl.ZljlInfo;
import com.app.domain.zl.ZljlRealData;
import com.app.domain.zl.Zlrw;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface ZlMapper {
    @Select("select * from zljl_data")
    public List<ZljlData> findAllZljlData();

    List<ZljlInfo> queryZljlInfoPage(String searchIsAll);


    @Update("update zljl_info set UPLOADED = #{uploaded}  where id = #{zljlInfoId}")
    void updateUploadedById(@Param("uploaded") String uploaded, @Param("zljlInfoId") Integer zljlInfoId);

    @Select("SELECT  * FROM zljl_info WHERE id = #{id}")
    ZljlInfo queryZljlInfoById(int id);

    @Select("select * from zljl_data where qlbh=#{qlbh}")
    List<ZljlData> findZljlDataByQlbh(String qlbh);

    @Select("select * from zljl_data where qlbh=#{qlbh} and gsbh =#{gsbh}")
    ZljlData getZljlDataByQlbhAndGsbh(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);

    @Select("select * from ZLJL_RealData where qlbh=#{qlbh} and gsbh  like  #{gsbh}")
    List<ZljlRealData> findRealDataByQlBhAndGsbh(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);

    @Select("select * from ZLJL_RealData where qlbh=#{qlbh}")
    List<ZljlRealData> findRealDataByQlBh(@Param("qlbh") String qlbh);


    @Select("select * from ZLJL_RealData where qlbh=#{qlbh} and gsbh = #{gsbh}")
    List<ZljlRealData> queryZljlRealData(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);
  /*  @Select("SELECT * FROM ZLJL_RealData where qlbh=#{qlbh} and gsbh in (#{gsbh1},#{gsbh2})")
    List<ZljlRealData> queryZljlRralDataByGsbh(@Param("qlbh") String qlbh, @Param("gsbh1") String gsbh1, @Param("gsbh2") String gsbh2);*/
    @Select("select TOP 1 * from ZLJL_RealData where qlbh=#{qlbh}")
    ZljlRealData getZljlRralDataTopOne(String qlbh);

    @Select("select TOP 1 * from ZLJL_RealData where qlbh=#{qlbh}  ORDER BY id DESC")
    ZljlRealData getZljlRralDataLastOne(String qlbh);

    @Select("select TOP 1 * from ZLJL_RealData where qlbh=#{qlbh} and gsbh = #{gsbh}")
    ZljlRealData getZljlRralDataGsbhTopOne(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);

    @Select("select TOP 1 * from ZLJL_RealData where qlbh=#{qlbh} and gsbh = #{gsbh}  ORDER BY id DESC")
    ZljlRealData getZljlRralDataGsbhLastOne(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);

    @Select("SELECT * FROM ZLJL_RealData where qlbh=#{qlbh} and gsbh in (#{gsbh1},#{gsbh2})")
    List<ZljlRealData> queryZljlRralDataByGsbh(@Param("qlbh") String qlbh, @Param("gsbh1") String gsbh1, @Param("gsbh2") String gsbh2);
    @Select("select * from ZLJL_RealData where qlbh=#{qlbh} and gsbh LIKE CONCAT( '%',#{gsbh}, '%')")
    List<ZljlRealData> findRealDataByQlBhLikeGsbh(@Param("qlbh") String qlbh, @Param("gsbh") String gsbh);

    @Select("SELECT  * FROM ZLRW_2 WHERE lh = #{lh}")
    List<Zlrw> queryZlrwListByLh(@Param("lh") String lh);
    
    // 查询uploadStatus为0的梁信息
    @Select("SELECT * FROM zljl_info WHERE uploaded = '2' OR uploaded = '0' OR uploaded IS NULL")
    List<ZljlInfo> findZljlInfoByUploadStatus();
    
    // 查询指定梁号已完成张拉的孔数量
    @Select("SELECT COUNT(*) FROM zljl_data WHERE qlbh = #{qlbh} AND zlzt = '张拉结束'")
    int countFinishedHolesByQlbh(@Param("qlbh") String qlbh);
    
    // 更新ZljlInfo的上传状态和时间
    @Update("UPDATE zljl_info SET uploaded = #{uploaded}, lastUploadedTime = #{lastUploadedTime} WHERE id = #{id}")
    void updateZljlInfoUploadStatus(@Param("id") int id, @Param("uploaded") String uploaded, @Param("lastUploadedTime") String lastUploadedTime);
    
    // 更新ZljlData的上传标识和时间
    @Update("UPDATE zljl_data SET uploadedFlag = #{uploadedFlag}, uploadTime = #{uploadTime}, syjid = #{syjid}, fguid = #{fguid} WHERE id = #{id}")
    void updateZljlDataUploadFlag(@Param("id") int id, @Param("uploadedFlag") String uploadedFlag, @Param("uploadTime") String uploadTime, @Param("syjid") String syjid, @Param("fguid") String fguid);

    // 查询未上传的已完成张拉的孔数据
    @Select("SELECT * FROM zljl_data WHERE qlbh = #{qlbh} AND zlzt = '张拉结束' AND (uploadedFlag IS NULL OR uploadedFlag = '0')")
    List<ZljlData> findUnuploadedFinishedHoles(@Param("qlbh") String qlbh);
    
    // 新增方法：插入上传记录
    @Update("INSERT INTO tension_upload_record (syjid, fguid, qlbh, gsbh, uploadTime, status) VALUES (#{syjid}, #{fguid}, #{qlbh}, #{gsbh}, #{uploadTime}, #{status})")
    void insertUploadRecord(TensionUploadRecord record);
    
    // 新增方法：插入上传状态记录
    @Insert("INSERT INTO zljl_data_upload_status (zljlDataId, uploadedFlag, uploadTime, syjid, fguid, mainDataStatus, subDataStatus, curveDataStatus, uploadedCurveCount, totalCurveCount) VALUES (#{zljlDataId}, #{uploadedFlag}, #{uploadTime}, #{syjid}, #{fguid}, #{mainDataStatus}, #{subDataStatus}, #{curveDataStatus}, #{uploadedCurveCount}, #{totalCurveCount})")
    void insertZljlDataUploadStatus(ZljlDataUploadStatus status);
    
    // 新增方法：更新上传状态记录
    @Update("UPDATE zljl_data_upload_status SET uploadedFlag = #{uploadedFlag}, uploadTime = #{uploadTime}, syjid = #{syjid}, fguid = #{fguid}, mainDataStatus = #{mainDataStatus}, subDataStatus = #{subDataStatus}, curveDataStatus = #{curveDataStatus}, uploadedCurveCount = #{uploadedCurveCount}, totalCurveCount = #{totalCurveCount} WHERE zljlDataId = #{zljlDataId}")
    void updateZljlDataUploadStatus(ZljlDataUploadStatus status);
    
    // 新增方法：根据zljlDataId查询上传状态
    @Select("SELECT * FROM zljl_data_upload_status WHERE zljlDataId = #{zljlDataId}")
    ZljlDataUploadStatus findZljlDataUploadStatusByZljlDataId(@Param("zljlDataId") int zljlDataId);
    
    // 新增方法：查询未上传的已完成张拉的孔数据(使用新的上传状态表)
    @Select("SELECT z.* FROM zljl_data z " +
            "LEFT JOIN zljl_data_upload_status s ON z.id = s.zljlDataId " +
            "WHERE z.qlbh = #{qlbh} AND z.zlzt = '张拉结束' AND (s.uploadedFlag IS NULL OR s.uploadedFlag = '0')")
    List<ZljlData> findUnuploadedFinishedHolesWithNewStatus(@Param("qlbh") String qlbh);
}